package com.example.test_app;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.example.test_app.bean.UserInfo;

public class WebActivity extends AppCompatActivity {
    private WebView webView;
    private TextView tvProgress;

    private String url="https://wtb.wanyoa.com/small/cmpaytoast?uid=28816907&token=Fa4b98huTwbPPm3+o7m6Hw==&app=186";
//    private String url="https://www.baidu.com/";


    @SuppressLint("JavascriptInterface")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);
        //webview 调试
        WebView.setWebContentsDebuggingEnabled(true);
        //获得控件
        webView = (WebView) findViewById(R.id.wv_webview);


        webView.getSettings().setJavaScriptEnabled(true);

        webView.getSettings().setPluginState(WebSettings.PluginState.ON);
        webView.requestFocus();
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setTextZoom(100);
        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setDomStorageEnabled(true);
        webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        //获得其他控件
        tvProgress = (TextView) findViewById(R.id.tv_progress);
        //访问网页
        webView.loadUrl(url);
//系统默认会通过手机浏览器打开网页，为了能够直接通过WebView显示网页，则必须设置

        //设置WebViewClient
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                //使用WebView加载显示url
                if (url.startsWith("baiduboxapp:")){
                    return true;
                }
                view.loadUrl(url);

//返回true
                return true;
            }

            //加载前
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                tvProgress.setText("开始加载！！");

            }

            //加载完成
            @Override
            public void onPageFinished(WebView view, String url) {

                tvProgress.setText("加载完成...");


            }
        });
//设置WebChromeClient类
        webView.setWebChromeClient(new WebChromeClient() {
            //获取网站标题
            @Override
            public void onReceivedTitle(WebView view, String title) {
                setTitle(title);
            }

            //进度显示
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress < 100) {
                    tvProgress.setText(newProgress + "%");
                } else {
                    tvProgress.setText("100%");
                }
            }
        });

        webView.addJavascriptInterface(new MyJavascriptInterface(this), "appInterface");
    }

    //点击返回上一页面而不是退出浏览器
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
            webView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    //销毁Webview
    @Override
    protected void onDestroy() {
        if (webView != null) {
            webView.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
            webView.clearHistory();
            ((ViewGroup) webView.getParent()).removeView(webView);
            webView.destroy();
            webView = null;
        }
        super.onDestroy();
    }
}
